package com.ddxz.tool.sql.generator;

import com.ddxz.tool.sql.generator.impl.BaseSqlGenerator;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;

/**
 * @description
 * @author 朱宗刚
 * @date 2019年10月23日
 * @version 1.0.0
 */

@Slf4j
public class SqlGeneratorFactory {
    public static synchronized SqlGenerator createGenerater(Class<? extends BaseSqlGenerator> clz) {
        if(null != instances.get(clz)) {
            return instances.get(clz);
        }

        try{
            Constructor<?> constructor = clz.getDeclaredConstructor();
            constructor.setAccessible(true);
            instances.put(clz, (SqlGenerator) constructor.newInstance());
            return instances.get(clz);
        } catch (Exception e) {
            log.error("create sql generator failed", e);
            throw new RuntimeException("create sql generator failed");
        }
    }

    private static Map<Class<?>, SqlGenerator> instances = new HashMap<>(6);
}
